package niuke;

import java.util.ArrayList;

/**
 * description:
 * author:张腾
 * date:2021-07-09
 */
public class NC38 {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        if (matrix==null || matrix.length==0) return res;
        int l=0,h=0;
        //有边界
        int r=matrix[0].length-1;
        //下边界
        int d=matrix.length-1;
        while (true){
            for (int i = l; i <= r; i++) res.add(matrix[h][i]);
            if (++h>d) break;
            for (int i = h; i <= d; i++) res.add(matrix[i][r]);
            if (--r<l) break;
            for (int i = r; i >= l; i--) res.add(matrix[d][i]);
            if (--d<h) break;
            for (int i = d; i >= h; i--) res.add(matrix[i][l]);
            if (++l>r) break;
        }
        return res;
    }
}
